package com.bistu.openlab.uofspreadsheet.xmlparse;

import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Namespace;

import com.bistu.openlab.uofspreadsheet.model.SheetCell;
import com.bistu.openlab.uofspreadsheet.namespace.NamespaceDeal;
import com.bistu.openlab.uofspreadsheet.util.TypeConvertor;
import com.bistu.openlab.uofspreadsheet.util.XmlDomHelper;

/*
 *@author xiaoguang.gao@oracle.com
 *@version 2013-5-9 
 */
public class SheetCellParse implements IXmlParse<SheetCell>{
	private static SheetCellParse parse = new SheetCellParse();
	private SheetCellParse(){}
	/**
	 * 
	 * @return
	 */
	public static SheetCellParse getInstance(){
		return parse;
	}

	@Override
	public SheetCell readFromElement(Element root) {
		SheetCell sheetCell = null;
		if(root!=null&&root.getName().equals("单元格_E7F2")){
			sheetCell = new SheetCell();
			//column number
			String strNum = XmlDomHelper.GetAttributeValue(root, "列号_E7BC");
			sheetCell.setCellNum(TypeConvertor.GetInt(strNum));
			//style reference
			sheetCell.setSytleReference(XmlDomHelper.GetAttributeValue(root, "式样引用_E7BD"));
			//text content
			Element txtEle = (Element)root.selectSingleNode(".//字:文本串_415B");
			if(txtEle!=null)
				sheetCell.setText(txtEle.getText());
		}
		return sheetCell;
	}

	@Override
	public Element writeToElement(SheetCell obj) {
		Element root = null;
		if(obj!=null){
			root = DocumentHelper.createElement("表:单元格_E7F2");
			NamespaceDeal.AddNamespace(root);
			root.addAttribute("列号_E7BC", obj.getCellNum()+"");
			root.addAttribute("式样引用_E7BD", obj.getSytleReference());
			Element dataEle = root.addElement("表:数据_E7B3");
			dataEle.addAttribute("类型_E7B6", "text");
			Element sentenceEle = dataEle.addElement("字:句_419D");
			Element textEle = sentenceEle.addElement("字:文本串_415B");
			textEle.setText(obj.getText());
		}		
		return root;
	}

}
